﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AcadPos.Entity;
using System.Data;
using Microsoft.Reporting.WebForms;

namespace AcadPosApp.Relatorios
{
    public partial class RelatorioCronogramaAulas : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtDtAula.Text = DateTime.Now.ToShortDateString();
                carregaEstados();
                Estado estado = ControlClass.CNEstado.carregarDadosEstadoSE();
                if(estado.IdEstado > 0)
                    ddlEstadoPesquisa.SelectedValue = estado.IdEstado.ToString();
                carregaPolo();                               
                pnlPesquisa.Attributes.Add("onkeypress", "return clickButton(event,'" + btnConsultar.ClientID + "')");
            }
        }

        private void carregaEstados()
        {
            ddlEstadoPesquisa.DataSource = ControlClass.CNEstado.listaTodosEstados();
            ddlEstadoPesquisa.DataTextField = "ESTADO";
            ddlEstadoPesquisa.DataValueField = "ID_ESTADO";
            ddlEstadoPesquisa.DataBind();
            //ddlEstadoPesquisa.Items.Insert(0, new ListItem("Todos", "Todos"));
        }

        private void carregaGridPesquisa()
        {
            gvwPesquisa.Visible = true;
            int idPolo = ddlPoloPesquisa.SelectedValue != "" ? Convert.ToInt32(ddlPoloPesquisa.SelectedValue) : 0;
            DateTime dtAula = Convert.ToDateTime(txtDtAula.Text);
            gvwPesquisa.DataSource = ControlClass.CNDisciplina.listaCronogramaAulasPorData(idPolo, dtAula);
            gvwPesquisa.DataBind();
            gvwPesquisa.SelectedIndex = -1;
        }

        protected void btnConsultar_Click(object sender, EventArgs e)
        {
            carregaGridPesquisa();
        }

        private void carregaPolo()
        {
            Polo polo = new Polo();
            if (ddlEstadoPesquisa.SelectedValue.ToString() != "Todos")
                polo.IdEstado = Convert.ToInt32(ddlEstadoPesquisa.SelectedValue);
            DataTable dt = ControlClass.CNPolo.listaPolosEstado(polo);
            ddlPoloPesquisa.DataSource = dt;
            ddlPoloPesquisa.DataTextField = "DESCRICAO";
            ddlPoloPesquisa.DataValueField = "ID_POLO";
            ddlPoloPesquisa.DataBind();
            if (dt.Rows.Count > 0)
            {
                ddlPoloPesquisa.Enabled = true;
                ddlPoloPesquisa.Items.Insert(0, new ListItem("Todos", "0"));
            }
            else
                ddlPoloPesquisa.Enabled = false;
        }

        protected void btnConsultar_Click1(object sender, EventArgs e)
        {
            carregaGridPesquisa();
        }

        protected void ddlEstadoPesquisa_SelectedIndexChanged(object sender, EventArgs e)
        {
            carregaPolo();
        }

        protected void btnImprimirRelatorio_Click(object sender, EventArgs e)
        {

            int idPolo = ddlPoloPesquisa.SelectedValue != "" ? Convert.ToInt32(ddlPoloPesquisa.SelectedValue) : 0;
            DateTime dtAula = Convert.ToDateTime(txtDtAula.Text);
            DataTable dtImp = ControlClass.CNDisciplina.listaCronogramaAulasPorData(idPolo, dtAula);
            if (dtImp.Rows.Count == 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", "bootbox.alert('Não há registros à serem impressos!', function() {});", true);
            }
            else
            {

                ReportViewer ReportViewer1 = new ReportViewer();
                ReportViewer1.Reset();
                ReportViewer1.ProcessingMode = ProcessingMode.Local;
                ReportViewer1.LocalReport.ReportPath = Server.MapPath(@"~/Relatorios/RelatorioCronogramaAulas.rdlc");
                ReportViewer1.LocalReport.DataSources.Clear();


                ReportParameter rp_dataAula = new ReportParameter("dataAula", Server.HtmlDecode(txtDtAula.Text.ToString()));
                ReportParameter rp_polo = new ReportParameter("polo", Server.HtmlDecode(ddlPoloPesquisa.SelectedItem.Text.ToString()));
                ReportParameter rp_estado = new ReportParameter("estado", Server.HtmlDecode(ddlEstadoPesquisa.SelectedItem.Text.ToString()));

                ReportParameter[] ReportParans = new ReportParameter[] { rp_polo, rp_dataAula, rp_estado };
                ReportViewer1.LocalReport.SetParameters(ReportParans);


                ReportDataSource rds1 = new ReportDataSource();
                rds1.Name = "DataSet1";
                rds1.Value = dtImp;

                ReportDataSource rds2 = new ReportDataSource();
                rds2.Name = "DataSet2";
                rds2.Value = ControlClass.CNParametros.getCabecalhoRelatorio();

                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.DataSources.Add(rds1);
                ReportViewer1.LocalReport.DataSources.Add(rds2);
                ReportViewer1.LocalReport.Refresh();

                #region Gerar PDF
                Warning[] warn = null;
                string[] streamids = null;
                string mimeType = "application/pdf";
                string encoding = string.Empty;
                string extension = string.Empty;
                byte[] byteViewer;

                byteViewer = ReportViewer1.LocalReport.Render("pdf", null, out mimeType, out encoding, out extension, out streamids, out warn);
                Response.Buffer = true;
                Response.Clear();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment; filename=CronogramaAulas.pdf");
                Response.BinaryWrite(byteViewer);
                Response.Flush();

                Response.End();
                Response.ClearContent();
                #endregion
            }
        }

    }
}